package com.llynosy.interview.controller.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.llynosy.interview.base.BaseController;
import com.llynosy.interview.mapper.ProblemMapper;
import com.llynosy.interview.po.Problem;
import com.llynosy.interview.po.Result;
import com.llynosy.interview.po.StatusCode;
import com.llynosy.interview.po.Tag;
import com.llynosy.interview.service.ProblemService;
import com.llynosy.interview.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RequestMapping("/api/problem")
@RestController()
public class APIProblemController extends BaseController<Problem>{

    @Override
    @PostMapping
    public Result addE(@RequestBody Problem problem) throws Exception {
        if(problem.getTagIds()==null||problem.getTagIds().length==0){
            throw  new RuntimeException("至少选择一个标签");
        }
        if(StringUtils.isEmpty(problem.getTitle())){
            throw  new RuntimeException("不能没有标题");
        }
        super.addE(problem);
        return new Result(true,StatusCode.OK,"新增成功");
    }

    @Autowired
    private TagService tagService;
    /**
     * 获得展示在首页的最热问题
     * @return JSON
     */
    @GetMapping("/hot")
    public Object findHotProblem(){
        List<Problem> hotProblems=((ProblemService)(this.commCrudService)).getHotProblem();
        List<Map<Problem,List<Tag>>> problemTags=new ArrayList<>(15);
        for(Problem hotProblem:hotProblems){
            Map<Problem,List<Tag>> map=new HashMap<>(15);
            List<Tag> tags=tagService.getTagByProblemId(hotProblem.getId());
            map.put(hotProblem,tags);
            problemTags.add(map);
        }
        return JSONArray.toJSONString(hotProblems);
       // return JSONArray.toJSONString(problemTags);
        //return new Result<List<Map<Problem,List<Tag>>>>(true,StatusCode.OK,"SUCCESS", problemTags);
        //return new Result<List<Map<Problem,List<Tag>>>>(true,StatusCode.OK,"SUCCESS",JSON.toJSONString(hotProblems));
    }

    /**
     * 获得展示在首页最新的问题
     * @return JSON
     */
    @GetMapping("/latest")
    public Result findLatestProblem(){
        List<Problem> latestProblems=((ProblemService)(this.commCrudService)).getLatestProblem();

        return new Result(true,StatusCode.OK,"SUCCESS",latestProblems);
    }

    @GetMapping("/tag/{tagid}/page/{page}")
    public Result findProblemByTag(@PathVariable("tagid")Long tagid, @PathVariable("page")Integer page){
        if(page==null||page>10000)
            page=1;
        List<Problem> problemList=((ProblemService)this.commCrudService).getProblemByTag(tagid,page);
        return new Result(true,StatusCode.OK,"SUCCESS",problemList);
    }

    /**
     * 获得某个标签下问题的数量
     *
     * @param tagid
     * @return
     */
    @GetMapping("/tag/{tagid}/total")
    public Result getNumberOfProblemsUnderTag(@PathVariable("tagid")Long tagid){
        Integer totalNumbers=((ProblemService)this.commCrudService).getNumberOfProblemsUnderTag(tagid);
        return new Result(true,StatusCode.OK,"SUCCESS",totalNumbers);
    }

    @GetMapping("/tag/{tagid}/hotProblem")
    public Result findHotProblemBelowATag(@PathVariable("tagid")Long tagid){
        List<Problem> aTagHotProblemList=((ProblemService)this.commCrudService).getHotProblemBelowATag(tagid);
        return new Result(true,StatusCode.OK,"SUCCESS",aTagHotProblemList);
    }

    @GetMapping("/{problemId}/relate")
    public Result findProblemRelatedProblems(@PathVariable("problemId")Long problemId){
        Set<Problem> relatedProblems=new HashSet<>();
        relatedProblems.addAll(((ProblemService)this.commCrudService).getProblemRelatedProblems(problemId));
        return new Result(true,StatusCode.OK,"SUCCESS",relatedProblems);
    }


//    @Override
//    @GetMapping("/{id}")
//    public Result<Problem> findById(@PathVariable("id") Long id) {
//        Problem problem=new Problem();
//        problem.setId(111L);
//        problem.setTitle("如何做一个优雅的舔狗？");
//        problem.setAnswerNumber(100);
//        return new Result<>(true,StatusCode.OK,"SUCCESS",problem);
//    }
}
